// pages/index/fenlan.js
var app = getApp();

Page({
    data: {
        flag: 0,
        filterData: [],
        searchContent: '',
        products: [],
        curPage: 1,
        app_flag: true
    },
    onLoad: function (options) {
        var that = this;
        console.log(wx.getSystemInfoSync().windowHeight)
        console.log()
        this.setData({
            windowHeight: wx.getSystemInfoSync().windowHeight - 180/wx.getSystemInfoSync().pixelRatio
        })
        this.tapShangp();
    },
    tapShangp() {
        this.setData({
            flag: 0,
            filterData: [],
            curPage: 1
        })
        this.search();
    },
    tapDianp() {
        this.setData({
            flag: 1,
            filterData: [],
            curPage: 1
        })
        this.search();
    },
    loadMore:function(){
        
        if (this.data.app_flag){
            var cur_page = this.data.curPage+1;
            this.setData({
                curPage: cur_page
            })
            console.log(this.data.curPage)
            this.search_curPage(this.data.curPage)
        }
        
    },
    search: function () {
        console.log(this.data.searchContent)
        this.setData({
            curPage: 1,
            filterData: [],
            app_flag: true
        })
        this.search_curPage(this.data.curPage);
    },
    search_curPage: function (curPage) {
        var type;
        this.data.flag == 0 ? type = "shangp" : type = "dianp";
        var that = this;
        console.log(type)
        app.func.req('search/' + type, {
            name: that.data.searchContent,
            page: curPage
        }, 'GET', function (res) {
            console.log(res);
            if(res.msg == 'index.users.empty'){
                that.setData({
                    app_flag: false
                })
                return false;
            }
            var filterData = that.data.filterData;
            if(type =="dianp"){   
                res.result.map(function (el) {
                    if(el)   filterData.push(el);
                })  
            }
            else {
                res.result.map(function (el) {
                    if (el) filterData.push(el);
                })  
                res.result.map(function(el){
                    el.product.map(function(e){
                        e.name.length>10?e.name=e.name.substr(0,10)+"...":e.name;
                    })
                })
            }
            console.log(filterData)
            that.setData({
                filterData: filterData
            })
        },
            {
                'token': wx.getStorageSync('token')
            })
    },
    change(e) {
        console.log(e);
        this.setData({
            searchContent: e.detail.value
        })
    }
})